#!/usr/bin/env bash


#!/bin/bash


source ../ovs_helper.sh

add_ovs_tap_pair br50 tap50_1 tap50_50 

add_ovs_tap_pair manage tap501_99 tap501_199 

add_ovs_tap_pair br199 tap199_254 tap199_50 

mkdir -p firmware

if [[ ! -e "./firmware/Adisk.qcow2" ]]; then

	qemu-img create -f qcow2 -b "$PWD/firmware/hda.qcow2" -F qcow2 "$PWD/firmware/Adisk.qcow2"
fi

if [[ ! -e "./firmware/Bdisk.qcow2" ]]; then

	qemu-img create -f qcow2 -b "$PWD/firmware/hda.qcow2" -F qcow2 "$PWD/firmware/Bdisk.qcow2"
fi



connect_lan1_script=$(create_switch lan1)
connect_lan2_script=$(create_switch lan2)
connect_lan3_script=$(create_switch lan3)


args=(

    # 尤其当心mac地址冲突
        -device virtio-net-pci,netdev=net0,mac=50:00:00:02:00:00
        -netdev tap,id=net0,ifname=ar1_0,script=$connect_lan1_script

        -device virtio-net-pci,netdev=net1,mac=50:00:00:02:00:01
        -netdev tap,id=net1,ifname=ar1_1,script=$connect_lan2_script

        -device virtio-net-pci,netdev=net2,mac=50:00:01:92:50:01
        -netdev tap,id=net2,ifname=ar1_2,script=$connect_lan3_script

        -device virtio-net-pci,netdev=net3,mac=50:00:10:50:01:99
        -netdev tap,id=net3,ifname=ar1_3,script=no

        -device virtio-net-pci,netdev=net4,mac=50:00:00:02:00:04
        -netdev tap,id=net4,ifname=ar1_4,script=no

        -device virtio-net-pci,netdev=net5,mac=50:00:00:02:00:05
        -netdev tap,id=net5,ifname=ar1_5,script=no

        # 这个路由器镜像是我自己下载的，不是来在eve懒人整合包
        -drive file=./firmware/Adisk.qcow2,if=virtio,bus=0,unit=0,cache=none 

	-serial mon:tcp::40001,server,nowait 
        )

boot(){


	local -n args_array=$1
	#/opt/qemu-2.12.0/bin/
	qemu-system-x86_64 \
        	-smp 1 -m 2048 \
		-name AR1000v -uuid 6d51adc0-0cd5-4f58-bf18-ffed3ff7e7e7 \
        	-machine type=pc,accel=kvm -vga std -usbdevice tablet \
		-boot order=cd -cpu host,vmx=off,svm=off \
		-display none \
		-daemonize \
        	${args_array[@]}

        	#-nographic \
}


switchB_args=$(echo ${args[@]} | sed 's/mac=50/mac=52/g' | sed 's/ar1/ar2/g' | sed 's/tap501_99/tap501_199/g' | sed 's/tap50_1/tap199_254/g' | sed 's/40001/40002/g' | sed 's/Adisk.qcow2/Bdisk.qcow2/g' ) 

boot args
boot switchB_args
